<?php

namespace app\common\model\xiluhouse;

use think\Model;
use traits\model\SoftDelete;

class Comment extends Base
{

    use SoftDelete;

    // 表名
    protected $name = 'xiluhouse_comment';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';
    protected $has_weigh = false;

    // 追加属性
    protected $append = [
        'images_arr',
        'createtime_text',
        'star_text',
        'type_text',
        'comment_type_text',
        'identity_type_text',
    ];
    // 处理多图的情况
    public function getImagesArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $images_arr = $value ? explode(',', $value) : [];
        foreach ($images_arr as $now_image_key => $now_image_value) {
            $images_arr[$now_image_key] = cdnurl($now_image_value,true);
        }
        return $images_arr;
    }
    // 获取是否点赞该评论
    public function getIsLikeAttr($value, $data)
    {
        $id = $data['id'];
        $user_id = 0;
        $request = \think\Request::instance();
        $token = $request->server('HTTP_TOKEN', $request->request('token', \think\Cookie::get('token')));
        if ($token) {
            $auth = \app\common\library\Auth::instance();
            $auth->init($token);
            if ($auth) {
                $user_id = $auth->id;
            }
        }
        if ($user_id) {
            $exist = CommentLike::where('comment_id', $id)->where('user_id', $user_id)->find();
            if ($exist) {
                return true;
            }
        }
        return false;
    }
    public function getStarTextAttr($value, $data)
    {
        $star = $data['star'];
        switch ($star) {
            case 1:
                $star_text = '非常差';
                break;
            case 2:
                $star_text = '差';
                break;
            case 3:
                $star_text = '一般';
                break;
            case 4:
                $star_text = '满意';
                break;
            default:
                $star_text = '非常满意';
                break;
        }
        return $star_text;
    }
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getCommentTypeList()
    {
        return ['1' => __('Comment_type 1'), '2' => __('Comment_type 2')];
    }

    public function getIdentityTypeList()
    {
        return ['1' => __('Identity_type 1'), '2' => __('Identity_type 2'), '3' => __('Identity_type 3'), '4' => __('Identity_type 4')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCommentTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['comment_type']) ? $data['comment_type'] : '');
        $list = $this->getCommentTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIdentityTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['identity_type']) ? $data['identity_type'] : '');
        $list = $this->getIdentityTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function house()
    {
        return $this->belongsTo('House', 'house_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function broker()
    {
        return $this->belongsTo('User', 'broker_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
